/**
 * 滚动的背景格格
 */
package kgame5.kgui.gameui {
import kgame5.kutil.Util;

import flash.display.MovieClip;
import flash.events.Event;

public class ScrollGridBG{
	//_____________Property_______________________
	private var mc_f:MovieClip;//装网格的容器
	
	private var BG_W:int,BG_H:int;
	private var 
		CC:int,
		RC:int;
	
	private var 
		gridLinkID:String;
	private var 
		gridW:int,
		gridH:int;
	private var 
		xspeed:Number,
		yspeed:Number;
		
	private var aGrid:Array;//保持各个mc_grid的引用
	//_________________Method___________________
	public function ScrollGridBG(mc_f:MovieClip,
		gridLinkID:String,
		xspeed:Number,yspeed:Number){
			
		this.mc_f=mc_f;
		
		this.BG_W=mc_f.width;
		this.BG_H=mc_f.height;
		
		this.gridLinkID=gridLinkID;
		var mc_grid:MovieClip=Util.AttachMC(gridLinkID);
		gridW=mc_grid.width;
		gridH=mc_grid.height;
		
		this.xspeed=xspeed;
		this.yspeed=yspeed;
			
		CC=Math.floor(BG_W/gridW)+2;
		RC=Math.floor(BG_H/gridH)+2;
		
		//把图片画出来
		aGrid=new Array();
		for(var r:int=0;r<RC;r++){
			for(var c:int=0;c<CC;c++){
				var mc_grid:MovieClip=Util.AttachMC(gridLinkID);
				mc_grid.x=c*gridW-gridW;
				mc_grid.y=r*gridH-gridH;
				aGrid.push(mc_grid);
			}
		}
	}
	public function Start(){
		mc_f.addEventListener(Event.ENTER_FRAME, efh);
	}
	public function Stop(){
		mc_f.removeEventListener(Event.ENTER_FRAME, efh);
	}
	
	public function efh(e:Event){
		mc_f.x+=xspeed;
		mc_f.y+=yspeed;
		if(mc_f.x>gridW){
			mc_f.x-=gridW;
		}
		if(mc_f.y>gridH){
			mc_f.y-=gridH;
		}
	}
	
	public function Clear(){
		for each(var mc_grid:MovieClip in aGrid){
			mc_f.removeChild(mc_grid);
		}
	}
}//end of class
}